//
//  ZyaFeedOrderAddAmountController.swift
//  SlaughterClient
//
//  Created by Zhouweibin on 2018/1/26.
//  Copyright © 2018年 zwb. All rights reserved.
//

import UIKit

class ZyaFeedOrderAddAmountController: UITableViewController {
    
    var order = [ZyaFeedOrder]()
    var isShowPrice = false
    var isUpdateOrder = false
    var backModeArrClosure:(([ZyaFeedOrder])->())?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "添加数量"
        
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 90
        tableView.separatorStyle = .none
        tableView.backgroundColor = Theme.tableViewColor
        tableView.register(ZyaFeedOrderAddAmountCell.self, forCellReuseIdentifier: ZyaFeedOrderAddAmountCell.identifier)
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "done"), style: .plain, target: self, action: #selector(addToShoppingCart))
        
    }
    
    @objc private func addToShoppingCart() {
        view.endEditing(false)
    
        let a = order.filter { $0.amount == "" }
        
        guard a.count == 0 else {
            if let name = a.first?.brandNm {
                KSToast("请输入\(name)数量")
            } else {
                KSToast("请输入数量")
            }
            return
        }
        
        let b = order.filter { $0.amount == "0" }
        
        guard b.count == 0 else {
            if let name = b.first?.brandNm {
                KSToast("数量不能为0，请重新输入\(name)数量")
            } else {
                KSToast("数量不能为0，请重新输入数量")
            }
            return
        }
        if let clo =  backModeArrClosure {
            clo(order)
            self.navigationController?.popViewController(animated: true)
        } else {
            let vc = ZyaFeedOrderDetailController()
            vc.order = order
            vc.isUpdateOrder = isUpdateOrder
            navigationController?.pushViewController(vc, animated: true)
        }
 
        
    }
    
}

extension ZyaFeedOrderAddAmountController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return order.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ZyaFeedOrderAddAmountCell.identifier) as? ZyaFeedOrderAddAmountCell else { fatalError() }
        cell.isShow = isShowPrice
        cell.model = order[indexPath.row]
        return cell
    }

}
